package com.beerandjesus.sportofkings.handicapper;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.beerandjesus.sportofkings.handicapper.app.CardAnalyzer;
import com.beerandjesus.sportofkings.handicapper.app.CardAnalyzerException;
import com.beerandjesus.sportofkings.handicapper.model.CardVO;
import com.beerandjesus.sportofkings.handicapper.model.RaceAnalysisResultVO;
import com.beerandjesus.sportofkings.handicapper.model.ResultsTable;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.CardVOImpl;

import java.util.List;

/**
 * Main class for -jar execution.
 *
 */
public class Main 
{
	private ApplicationContext context;
    private String filePath;

    private CardAnalyzer cardAnalyzer;
	
    public static void main( String[] args ) {
        if (args == null || args.length != 1) {
            System.out.println("Usage: java -jar <jarfile> <path to past performances file>");
            return;
        }
        Main main = new Main();
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        main.setApplicationContext(context);
        main.setFilePath(args[0]);
        main.execute();
    }
    
    public void execute() {
        cardAnalyzer = (CardAnalyzer) context.getBean("cardAnalyzer");
        CardVOImpl card = new CardVOImpl();
        card.setFilePath(getFilePath());
        List<RaceAnalysisResultVO> races;
        try {
            races = cardAnalyzer.analyzeCard(card);
        } catch (CardAnalyzerException e) {
            e.printStackTrace();
            return;
        }
        for (RaceAnalysisResultVO race : races) {
            System.out.println(race.getTrackCode() + " #" + race.getRaceNumber() + ": " + race.getRaceName() + " " +
                race.getPurse());
            System.out.println("Surface: " + race.getSurface().getDescription() + "; Par: " + race.getSpeedPar());
            System.out.println(race.getDistance());
            System.out.println("");
            ResultsTable t = race.getResultsTable();
            System.out.print(t.toStringByType());
            System.out.println("");
            System.out.println("");
        }

    }
    
    public void setApplicationContext(ApplicationContext context) {
    	this.context = context;
    }
    
    public ApplicationContext getApplicationContext() {
    	return context;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}
